home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / libtiff / tif_lzw.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  32KB  |  1,240 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/libtiff/RCS/tif_lzw.c,v 1.46 93/01/28 16:59:24 sam Rel $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. /*
  30.  * TIFF Library.
  31.  * Rev 5.0 Lempel-Ziv & Welch Compression Support
  32.  *
  33.  * This code is derived from the compress program whose code is
  34.  * derived from software contributed to Berkeley by James A. Woods,
  35.  * derived from original work by Spencer Thomas and Joseph Orost.
  36.  *
  37.  * The original Berkeley copyright notice appears below in its entirety.
  38.  */
  39. #include "tiffiop.h"
  40. #include <assert.h>
  41. #include <stdio.h>
  42.  
  43. /*
  44.  * NB: The 5.0 spec describes a different algorithm than Aldus
  45.  *     implements.  Specifically, Aldus does code length transitions
  46.  *     one code earlier than should be done (for real LZW).
  47.  *     Earlier versions of this library implemented the correct
  48.  *     LZW algorithm, but emitted codes in a bit order opposite
  49.  *     to the TIFF spec.  Thus, to maintain compatibility w/ Aldus
  50.  *     we interpret MSB-LSB ordered codes to be images written w/
  51.  *     old versions of this library, but otherwise adhere to the
  52.  *     Aldus "off by one" algorithm.
  53.  *
  54.  * Future revisions to the TIFF spec are expected to "clarify this issue".
  55.  */
  56. #define    LZW_COMPAT        /* include backwards compatibility code */
  57. /*
  58.  * Each strip of data is supposed to be terminated by a CODE_EOI.
  59.  * If the following #define is included, the decoder will also
  60.  * check for end-of-strip w/o seeing this code.  This makes the
  61.  * library more robust, but also slower.
  62.  */
  63. #define    LZW_CHECKEOS        /* include checks for strips w/o EOI code */
  64.  
  65. #define MAXCODE(n)    ((1<<(n))-1)
  66. /*
  67.  * The TIFF spec specifies that encoded bit
  68.  * strings range from 9 to 12 bits.
  69.  */
  70. #define    BITS_MIN    9        /* start with 9 bits */
  71. #define    BITS_MAX    12        /* max of 12 bit strings */
  72. /* predefined codes */
  73. #define    CODE_CLEAR    256        /* code to clear string table */
  74. #define    CODE_EOI    257        /* end-of-information code */
  75. #define CODE_FIRST    258        /* first free code entry */
  76. #define    CODE_MAX    MAXCODE(BITS_MAX)
  77. #define    HSIZE        9001        /* 91% occupancy */
  78. #define    HSHIFT        (13-8)
  79. #ifdef LZW_COMPAT
  80. /* NB: +1024 is for compatibility with old files */
  81. #define    CSIZE        (MAXCODE(BITS_MAX)+1024)
  82. #else
  83. #define    CSIZE        (MAXCODE(BITS_MAX)+1)
  84. #endif
  85.  
  86. #if USE_PROTOTYPES
  87. typedef    void (*predictorFunc)(char* data, u_long nbytes, int stride);
  88. #else
  89. typedef    void (*predictorFunc)();
  90. #endif
  91.  
  92. /*
  93.  * State block for each open TIFF
  94.  * file using LZW compression/decompression.
  95.  */
  96. typedef    struct {
  97.     predictorFunc hordiff;        /* horizontal differencing method */
  98.     u_long    rowsize;        /* width of tile/strip/row */
  99.     u_short    stride;            /* horizontal diferencing stride */
  100.     u_short    nbits;            /* # of bits/code */
  101.     u_short    maxcode;        /* maximum code for lzw_nbits */
  102.     u_short    free_ent;        /* next free entry in hash table */
  103.     long    nextdata;        /* next bits of i/o */
  104.     long    nextbits;        /* # of valid bits in lzw_nextdata */
  105. } LZWState;
  106.  
  107. #define    lzw_hordiff    base.hordiff
  108. #define    lzw_rowsize    base.rowsize
  109. #define    lzw_stride    base.stride
  110. #define    lzw_nbits    base.nbits
  111. #define    lzw_maxcode    base.maxcode
  112. #define    lzw_free_ent    base.free_ent
  113. #define    lzw_nextdata    base.nextdata
  114. #define    lzw_nextbits    base.nextbits
  115.  
  116. /*
  117.  * Decoding-specific state.
  118.  */
  119. typedef struct code_ent {
  120.     struct code_ent *next;
  121.     u_short    length;            /* string len, including this token */
  122.     u_char    value;            /* data value */
  123.     u_char    firstchar;        /* first token of string */
  124. } code_t;
  125.  
  126. #if USE_PROTOTYPES
  127. typedef    int (*decodeFunc)(TIFF*, u_char*, u_long, u_int);
  128. #else
  129. typedef    int (*decodeFunc)();
  130. #endif
  131.  
  132. typedef struct {
  133.     LZWState base;
  134.     long    dec_nbitsmask;        /* lzw_nbits 1 bits, right adjusted */
  135.     long    dec_restart;        /* restart count */
  136. #ifdef LZW_CHECKEOS
  137.     long    dec_bitsleft;        /* available bits in raw data */
  138. #endif
  139.     decodeFunc dec_decode;        /* regular or backwards compatible */
  140.     code_t    *dec_codep;        /* current recognized code */
  141.     code_t    *dec_oldcodep;        /* previously recognized code */
  142.     code_t    *dec_free_entp;        /* next free entry */
  143.     code_t    *dec_maxcodep;        /* max available entry */
  144.     code_t    dec_codetab[CSIZE];
  145. } LZWDecodeState;
  146.  
  147. /*
  148.  * Encoding-specific state.
  149.  */
  150. typedef struct {
  151.     long    hash;
  152.     long    code;
  153. } hash_t;
  154.  
  155. typedef struct {
  156.     LZWState base;
  157.     int    enc_oldcode;        /* last code encountered */
  158.     long    enc_checkpoint;        /* point at which to clear table */
  159. #define CHECK_GAP    10000        /* enc_ratio check interval */
  160.     long    enc_ratio;        /* current compression ratio */
  161.     long    enc_incount;        /* (input) data bytes encoded */
  162.     long    enc_outcount;        /* encoded (output) bytes */
  163.     char    *enc_rawlimit;        /* bound on tif_rawdata buffer */
  164.     hash_t    enc_hashtab[HSIZE];
  165. } LZWEncodeState;
  166.  
  167. #if USE_PROTOTYPES
  168. static    int LZWEncodePredRow(TIFF*, u_char*, u_long, u_int);
  169. static    int LZWEncodePredTile(TIFF*, u_char*, u_long, u_int);
  170. static    int LZWDecode(TIFF*, u_char*, u_long, u_int);
  171. #ifdef LZW_COMPAT
  172. static    int LZWDecodeCompat(TIFF*, u_char*, u_long, u_int);
  173. #endif
  174. static    int LZWDecodePredRow(TIFF*, u_char*, u_long, u_int);
  175. static    int LZWDecodePredTile(TIFF*, u_char*, u_long, u_int);
  176. static    void cl_hash(LZWEncodeState*);
  177. #else
  178. static    int LZWDecode();
  179. static    int LZWEncodePredRow();
  180. static    int LZWEncodePredTile();
  181. static    int LZWDecode();
  182. #ifdef LZW_COMPAT
  183. static    int LZWDecodeCompat();
  184. #endif
  185. static    int LZWDecodePredRow();
  186. static    int LZWDecodePredTile();
  187. static    void cl_hash();
  188. #endif
  189.  
  190. static
  191. DECLARE4(LZWCheckPredictor,
  192.     TIFF*, tif,
  193.     LZWState*, sp,
  194.     predictorFunc, pred8bit,
  195.     predictorFunc, pred16bit
  196. )
  197. {
  198.     TIFFDirectory *td = &tif->tif_dir;
  199.  
  200.     sp->hordiff = NULL;
  201.     switch (td->td_predictor) {
  202.     case 1:
  203.         break;
  204.     case 2:
  205.         sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  206.             td->td_samplesperpixel : 1);
  207.         switch (td->td_bitspersample) {
  208.         case 8:
  209.             sp->hordiff = pred8bit;
  210.             break;
  211.         case 16:
  212.             sp->hordiff = pred16bit;
  213.             break;
  214.         default:
  215.             TIFFError(tif->tif_name,
  216.     "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
  217.                 td->td_bitspersample);
  218.             return (0);
  219.         }
  220.         break;
  221.     default:
  222.         TIFFError(tif->tif_name, "\"Predictor\" value %d not supported",
  223.             td->td_predictor);
  224.         return (0);
  225.     }
  226.     if (sp->hordiff != NULL) {
  227.         /*
  228.          * Calculate the scanline/tile-width size in bytes.
  229.          */
  230.         if (isTiled(tif))
  231.             sp->rowsize = TIFFTileRowSize(tif);
  232.         else
  233.             sp->rowsize = TIFFScanlineSize(tif);
  234.     } else
  235.         sp->rowsize = 0;
  236.     return (1);
  237. }
  238.  
  239. /*
  240.  * LZW Decoder.
  241.  */
  242.  
  243. #ifdef LZW_CHECKEOS
  244. /*
  245.  * This check shouldn't be necessary because each
  246.  * strip is suppose to be terminated with CODE_EOI.
  247.  */
  248. #define    NextCode(tif, sp, bp, code, get) {                \
  249.     if ((sp)->dec_bitsleft < nbits) {                \
  250.         TIFFWarning(tif->tif_name,                \
  251.             "LZWDecode: Strip %d not terminated with EOI code", \
  252.             tif->tif_curstrip);                    \
  253.         code = CODE_EOI;                    \
  254.     } else {                            \
  255.         get(sp, bp, code);                    \
  256.         (sp)->dec_bitsleft -= nbits;                \
  257.     }                                \
  258. }
  259. #else
  260. #define    NextCode(tif, sp, bp, code, get) get(sp, bp, code)
  261. #endif
  262.  
  263. #define REPEAT4(n, op)        \
  264.     switch (n) {        \
  265.     default: { int i; for (i = n-4; i > 0; i--) { op; } } \
  266.     case 4:  op;        \
  267.     case 3:  op;        \
  268.     case 2:  op;        \
  269.     case 1:  op;        \
  270.     case 0:  ;            \
  271.     }
  272. #define XREPEAT4(n, op)        \
  273.     switch (n) {        \
  274.     default: { int i; for (i = n-4; i > 0; i--) { op; } } \
  275.     case 2:  op;        \
  276.     case 1:  op;        \
  277.     case 0:  ;            \
  278.     }
  279.  
  280. static void
  281. DECLARE3(horizontalAccumulate8,
  282.     register char*, cp,
  283.     register u_long, cc,
  284.     register int, stride
  285. )
  286. {
  287.     if (cc > stride) {
  288.         cc -= stride;
  289.         /*
  290.          * Pipeline the most common cases.
  291.          */
  292.         if (stride == 3)  {
  293.             u_int cr = cp[0];
  294.             u_int cg = cp[1];
  295.             u_int cb = cp[2];
  296.             do {
  297.                 cc -= 3, cp += 3;
  298.                 cp[0] = (cr += cp[0]);
  299.                 cp[1] = (cg += cp[1]);
  300.                 cp[2] = (cb += cp[2]);
  301.             } while ((long)cc > 0);
  302.         } else if (stride == 4)  {
  303.             u_int cr = cp[0];
  304.             u_int cg = cp[1];
  305.             u_int cb = cp[2];
  306.             u_int ca = cp[3];
  307.             do {
  308.                 cc -= 4, cp += 4;
  309.                 cp[0] = (cr += cp[0]);
  310.                 cp[1] = (cg += cp[1]);
  311.                 cp[2] = (cb += cp[2]);
  312.                 cp[3] = (ca += cp[3]);
  313.             } while ((long)cc > 0);
  314.         } else  {
  315.             do {
  316.                 XREPEAT4(stride, cp[stride] += *cp; cp++)
  317.                 cc -= stride;
  318.             } while ((long)cc > 0);
  319.         }
  320.     }
  321. }
  322.  
  323. static void
  324. DECLARE3(horizontalAccumulate16, char*, cp, u_long, cc, register int, stride)
  325. {
  326.     register short* wp = (short *)cp;
  327.     register u_long wc = cc / 2;
  328.  
  329.     if (wc > stride) {
  330.         wc -= stride;
  331.         do {
  332.             REPEAT4(stride, wp[stride] += wp[0]; wp++)
  333.             wc -= stride;
  334.         } while (wc > 0);
  335.     }
  336. }
  337.  
  338. /*
  339.  * Setup state for decoding a strip.
  340.  */
  341. static
  342. DECLARE1(LZWPreDecode, TIFF*, tif)
  343. {
  344.     register LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  345.  
  346.     if (sp == NULL) {
  347.         tif->tif_data = _TIFFmalloc(sizeof (LZWDecodeState));
  348.         if (tif->tif_data == NULL) {
  349.             TIFFError("LZWPreDecode",
  350.                 "No space for LZW state block");
  351.             return (0);
  352.         }
  353.         sp = (LZWDecodeState *)tif->tif_data;
  354.         sp->dec_decode = NULL;
  355.         if (!LZWCheckPredictor(tif, &sp->base,
  356.           horizontalAccumulate8, horizontalAccumulate16))
  357.             return (0);
  358.         if (sp->lzw_hordiff) {
  359.             /*
  360.              * Override default decoding method with
  361.              * one that does the predictor stuff.
  362.              */
  363.             tif->tif_decoderow = LZWDecodePredRow;
  364.             tif->tif_decodestrip = LZWDecodePredTile;
  365.             tif->tif_decodetile = LZWDecodePredTile;
  366.         }
  367.         /*
  368.          * Pre-load the table.
  369.          */
  370.         { int code;
  371.           for (code = 255; code >= 0; code--) {
  372.             sp->dec_codetab[code].value = code;
  373.             sp->dec_codetab[code].firstchar = code;
  374.             sp->dec_codetab[code].length = 1;
  375.             sp->dec_codetab[code].next = NULL;
  376.           }
  377.         }
  378.     }
  379.     /*
  380.      * Check for old bit-reversed codes.
  381.      */
  382.     if (tif->tif_rawdata[0] == 0 && (tif->tif_rawdata[1] & 0x1)) {
  383. #ifdef LZW_COMPAT
  384.         if (!sp->dec_decode) {
  385.             if (sp->lzw_hordiff == NULL) {
  386.                 /*
  387.                  * Override default decoding methods with
  388.                  * ones that deal with the old coding.
  389.                  * Otherwise the predictor versions set
  390.                  * above will call the compatibility routines
  391.                  * through the dec_decode method.
  392.                  */
  393.                 tif->tif_decoderow = LZWDecodeCompat;
  394.                 tif->tif_decodestrip = LZWDecodeCompat;
  395.                 tif->tif_decodetile = LZWDecodeCompat;
  396.             }
  397.             TIFFWarning(tif->tif_name,
  398.                 "Old-style LZW codes, convert file");
  399.         }
  400.         sp->lzw_maxcode = MAXCODE(BITS_MIN);
  401.         sp->dec_decode = LZWDecodeCompat;
  402. #else /* !LZW_COMPAT */
  403.         if (!sp->dec_decode) {
  404.             TIFFError(tif->tif_name,
  405.                 "Old-style LZW codes not supported");
  406.             sp->dec_decode = LZWDecode;
  407.         }
  408.         return (0);
  409. #endif/* !LZW_COMPAT */
  410.     } else {
  411.         sp->lzw_maxcode = MAXCODE(BITS_MIN)-1;
  412.         sp->dec_decode = LZWDecode;
  413.     }
  414.     sp->lzw_nbits = BITS_MIN;
  415.     sp->lzw_nextbits = 0;
  416.     sp->lzw_nextdata = 0;
  417.  
  418.     sp->dec_restart = 0;
  419.     sp->dec_nbitsmask = MAXCODE(BITS_MIN);
  420. #ifdef LZW_CHECKEOS
  421.     sp->dec_bitsleft = tif->tif_rawdatasize << 3;
  422. #endif
  423.     sp->dec_free_entp = sp->dec_codetab + CODE_FIRST;
  424.     sp->dec_oldcodep = &sp->dec_codetab[-1];
  425.     sp->dec_maxcodep = &sp->dec_codetab[sp->dec_nbitsmask-1];
  426.     return (1);
  427. }
  428.  
  429. /*
  430.  * Decode a "hunk of data".
  431.  */
  432. #define    GetNextCode(sp, bp, code) {                \
  433.     nextdata = (nextdata<<8) | *(bp)++;            \
  434.     nextbits += 8;                        \
  435.     if (nextbits < nbits) {                    \
  436.         nextdata = (nextdata<<8) | *(bp)++;        \
  437.         nextbits += 8;                    \
  438.     }                            \
  439.     code = (nextdata >> (nextbits-nbits)) & nbitsmask;    \
  440.     nextbits -= nbits;                    \
  441. }
  442.  
  443. static void
  444. DECLARE1(codeLoop, TIFF*, tif)
  445. {
  446.     TIFFError(tif->tif_name,
  447.         "LZWDecode: Bogus encoding, loop in the code table; scanline %d",
  448.         tif->tif_row);
  449. }
  450.  
  451. static int
  452. DECLARE4(LZWDecode, TIFF*, tif, u_char*, op0, u_long, occ0, u_int, s)
  453. {
  454.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  455.     char *op = (char *)op0;
  456.     long occ = (long)occ0;
  457.     char *tp;
  458.     u_char *bp;
  459.     int code, nbits, nextbits, len;
  460.     long nextdata, nbitsmask;
  461.     code_t *codep, *free_entp, *maxcodep, *oldcodep;
  462.  
  463.     assert(sp != NULL);
  464.     /*
  465.      * Restart interrupted output operation.
  466.      */
  467.     if (sp->dec_restart) {
  468.         int residue;
  469.  
  470.         codep = sp->dec_codep;
  471.         residue = codep->length - sp->dec_restart;
  472.         if (residue > occ) {
  473.             /*
  474.              * Residue from previous decode is sufficient
  475.              * to satisfy decode request.  Skip to the
  476.              * start of the decoded string, place decoded
  477.              * values in the output buffer, and return.
  478.              */
  479.             sp->dec_restart += occ;
  480.             do {
  481.                 codep = codep->next;
  482.             } while (--residue > occ && codep);
  483.             if (codep) {
  484.                 tp = op + occ;
  485.                 do {
  486.                     *--tp = codep->value;
  487.                     codep = codep->next;
  488.                 } while (--occ && codep);
  489.             }
  490.             return (1);
  491.         }
  492.         /*
  493.          * Residue satisfies only part of the decode request.
  494.          */
  495.         op += residue, occ -= residue;
  496.         tp = op;
  497.         do {
  498.             int t;
  499.             --tp;
  500.             t = codep->value;
  501.             codep = codep->next;
  502.             *tp = t;
  503.         } while (--residue && codep);
  504.         sp->dec_restart = 0;
  505.     }
  506.  
  507.     bp = (u_char *)tif->tif_rawcp;
  508.     nbits = sp->lzw_nbits;
  509.     nextdata = sp->lzw_nextdata;
  510.     nextbits = sp->lzw_nextbits;
  511.     nbitsmask = sp->dec_nbitsmask;
  512.     oldcodep = sp->dec_oldcodep;
  513.     free_entp = sp->dec_free_entp;
  514.     maxcodep = sp->dec_maxcodep;
  515.  
  516.     while (occ > 0) {
  517.         NextCode(tif, sp, bp, code, GetNextCode);
  518.         if (code == CODE_EOI)
  519.             break;
  520.         if (code == CODE_CLEAR) {
  521.             free_entp = sp->dec_codetab + CODE_FIRST;
  522.             nbits = BITS_MIN;
  523.             nbitsmask = MAXCODE(BITS_MIN);
  524.             maxcodep = sp->dec_codetab + nbitsmask-1;
  525.             NextCode(tif, sp, bp, code, GetNextCode);
  526.             if (code == CODE_EOI)
  527.                 break;
  528.             *op++ = code, occ--;
  529.             oldcodep = sp->dec_codetab + code;
  530.             continue;
  531.         }
  532.         codep = sp->dec_codetab + code;
  533.  
  534.         /*
  535.           * Add the new entry to the code table.
  536.           */
  537.         assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]);
  538.         free_entp->next = oldcodep;
  539.         free_entp->firstchar = free_entp->next->firstchar;
  540.         free_entp->length = free_entp->next->length+1;
  541.         free_entp->value = (codep < free_entp) ?
  542.             codep->firstchar : free_entp->firstchar;
  543.         if (++free_entp > maxcodep) {
  544.             if (++nbits > BITS_MAX)        /* should not happen */
  545.                 nbits = BITS_MAX;
  546.             nbitsmask = MAXCODE(nbits);
  547.             maxcodep = sp->dec_codetab + nbitsmask-1;
  548.         }
  549.         oldcodep = codep;
  550.         if (code >= 256) {
  551.             /*
  552.               * Code maps to a string, copy string
  553.              * value to output (written in reverse).
  554.               */
  555.             if (codep->length > occ) {
  556.                 /*
  557.                  * String is too long for decode buffer,
  558.                  * locate portion that will fit, copy to
  559.                  * the decode buffer, and setup restart
  560.                  * logic for the next decoding call.
  561.                  */
  562.                 sp->dec_codep = codep;
  563.                 do {
  564.                     codep = codep->next;
  565.                 } while (codep && codep->length > occ);
  566.                 if (codep) {
  567.                     sp->dec_restart = occ;
  568.                     tp = op + occ;
  569.                     do  {
  570.                         *--tp = codep->value;
  571.                         codep = codep->next;
  572.                     }  while (--occ && codep);
  573.                     if (codep)
  574.                         codeLoop(tif);
  575.                 }
  576.                 break;
  577.             }
  578.             len = codep->length;
  579.             tp = op + len;
  580.             do {
  581.                 int t;
  582.                 --tp;
  583.                 t = codep->value;
  584.                 codep = codep->next;
  585.                 *tp = t;
  586.             } while (codep && tp > op);
  587.             if (codep) {
  588.                 codeLoop(tif);
  589.                 break;
  590.             }
  591.             op += len, occ -= len;
  592.         } else
  593.             *op++ = code, occ--;
  594.     }
  595.  
  596.     tif->tif_rawcp = (char *)bp;
  597.     sp->lzw_nbits = nbits;
  598.     sp->lzw_nextdata = nextdata;
  599.     sp->lzw_nextbits = nextbits;
  600.     sp->dec_nbitsmask = nbitsmask;
  601.     sp->dec_oldcodep = oldcodep;
  602.     sp->dec_free_entp = free_entp;
  603.     sp->dec_maxcodep = maxcodep;
  604.  
  605.     if (occ > 0) {
  606.         TIFFError(tif->tif_name,
  607.         "LZWDecode: Not enough data at scanline %d (short %d bytes)",
  608.             tif->tif_row, occ);
  609.         return (0);
  610.     }
  611.     return (1);
  612. }
  613.  
  614. #ifdef LZW_COMPAT
  615. /*
  616.  * Decode a "hunk of data" for old images.
  617.  */
  618. #define    GetNextCodeCompat(sp, bp, code) {            \
  619.     nextdata |= *(bp)++ << nextbits;            \
  620.     nextbits += 8;                        \
  621.     if (nextbits < nbits) {                    \
  622.         nextdata |= *(bp)++ << nextbits;        \
  623.         nextbits += 8;                    \
  624.     }                            \
  625.     code = nextdata & nbitsmask;                \
  626.     nextdata >>= nbits;                    \
  627.     nextbits -= nbits;                    \
  628. }
  629.  
  630. static
  631. DECLARE4(LZWDecodeCompat, TIFF*, tif, u_char*, op0, u_long, occ0, u_int, s)
  632. {
  633.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  634.     char *op = (char *)op0;
  635.     long occ = occ0;
  636.     char *tp;
  637.     u_char *bp;
  638.     int code, nbits, nextbits;
  639.     long nextdata, nbitsmask;
  640.     code_t *codep, *free_entp, *maxcodep, *oldcodep;
  641.  
  642.     assert(sp != NULL);
  643.     /*
  644.      * Restart interrupted output operation.
  645.      */
  646.     if (sp->dec_restart) {
  647.         int residue;
  648.  
  649.         codep = sp->dec_codep;
  650.         residue = codep->length - sp->dec_restart;
  651.         if (residue > occ) {
  652.             /*
  653.              * Residue from previous decode is sufficient
  654.              * to satisfy decode request.  Skip to the
  655.              * start of the decoded string, place decoded
  656.              * values in the output buffer, and return.
  657.              */
  658.             sp->dec_restart += occ;
  659.             do {
  660.                 codep = codep->next;
  661.             } while (--residue > occ);
  662.             tp = op + occ;
  663.             do {
  664.                 *--tp = codep->value;
  665.                 codep = codep->next;
  666.             } while (--occ);
  667.             return (1);
  668.         }
  669.         /*
  670.          * Residue satisfies only part of the decode request.
  671.          */
  672.         op += residue, occ -= residue;
  673.         tp = op;
  674.         do {
  675.             *--tp = codep->value;
  676.             codep = codep->next;
  677.         } while (--residue);
  678.         sp->dec_restart = 0;
  679.     }
  680.  
  681.     bp = (u_char *)tif->tif_rawcp;
  682.     nbits = sp->lzw_nbits;
  683.     nextdata = sp->lzw_nextdata;
  684.     nextbits = sp->lzw_nextbits;
  685.     nbitsmask = sp->dec_nbitsmask;
  686.     oldcodep = sp->dec_oldcodep;
  687.     free_entp = sp->dec_free_entp;
  688.     maxcodep = sp->dec_maxcodep;
  689.  
  690.     while (occ > 0) {
  691.         NextCode(tif, sp, bp, code, GetNextCodeCompat);
  692.         if (code == CODE_EOI)
  693.             break;
  694.         if (code == CODE_CLEAR) {
  695.             free_entp = sp->dec_codetab + CODE_FIRST;
  696.             nbits = BITS_MIN;
  697.             nbitsmask = MAXCODE(BITS_MIN);
  698.             maxcodep = sp->dec_codetab + nbitsmask;
  699.             NextCode(tif, sp, bp, code, GetNextCodeCompat);
  700.             if (code == CODE_EOI)
  701.                 break;
  702.             *op++ = code, occ--;
  703.             oldcodep = sp->dec_codetab + code;
  704.             continue;
  705.         }
  706.         codep = sp->dec_codetab + code;
  707.  
  708.         /*
  709.           * Add the new entry to the code table.
  710.           */
  711.         assert(&sp->dec_codetab[0] <= free_entp && free_entp < &sp->dec_codetab[CSIZE]);
  712.         free_entp->next = oldcodep;
  713.         free_entp->firstchar = free_entp->next->firstchar;
  714.         free_entp->length = free_entp->next->length+1;
  715.         free_entp->value = (codep < free_entp) ?
  716.             codep->firstchar : free_entp->firstchar;
  717.         if (++free_entp > maxcodep) {
  718.             if (++nbits > BITS_MAX)        /* should not happen */
  719.                 nbits = BITS_MAX;
  720.             nbitsmask = MAXCODE(nbits);
  721.             maxcodep = sp->dec_codetab + nbitsmask;
  722.         }
  723.         oldcodep = codep;
  724.         if (code >= 256) {
  725.             /*
  726.               * Code maps to a string, copy string
  727.              * value to output (written in reverse).
  728.               */
  729.             if (codep->length > occ) {
  730.                 /*
  731.                  * String is too long for decode buffer,
  732.                  * locate portion that will fit, copy to
  733.                  * the decode buffer, and setup restart
  734.                  * logic for the next decoding call.
  735.                  */
  736.                 sp->dec_codep = codep;
  737.                 do {
  738.                     codep = codep->next;
  739.                 } while (codep->length > occ);
  740.                 sp->dec_restart = occ;
  741.                 tp = op + occ;
  742.                 do  {
  743.                     *--tp = codep->value;
  744.                     codep = codep->next;
  745.                 }  while (--occ);
  746.                 break;
  747.             }
  748.             op += codep->length, occ -= codep->length;
  749.             tp = op;
  750.             do {
  751.                 *--tp = codep->value;
  752.             } while (codep = codep->next);
  753.         } else
  754.             *op++ = code, occ--;
  755.     }
  756.  
  757.     tif->tif_rawcp = (char *)bp;
  758.     sp->lzw_nbits = nbits;
  759.     sp->lzw_nextdata = nextdata;
  760.     sp->lzw_nextbits = nextbits;
  761.     sp->dec_nbitsmask = nbitsmask;
  762.     sp->dec_oldcodep = oldcodep;
  763.     sp->dec_free_entp = free_entp;
  764.     sp->dec_maxcodep = maxcodep;
  765.  
  766.     if (occ > 0) {
  767.         TIFFError(tif->tif_name,
  768.         "LZWDecodeCompat: Not enough data at scanline %d (short %d bytes)",
  769.             tif->tif_row, occ);
  770.         return (0);
  771.     }
  772.     return (1);
  773. }
  774. #endif /* LZW_COMPAT */
  775.  
  776. /*
  777.  * Decode a scanline and apply the predictor routine.
  778.  */
  779. static
  780. DECLARE4(LZWDecodePredRow, TIFF*, tif, u_char*, op0, u_long, occ0, u_int, s)
  781. {
  782.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  783.  
  784.     assert(sp != NULL);
  785.     assert(sp->dec_decode != NULL);
  786.     if ((*sp->dec_decode)(tif, op0, occ0, s)) {
  787.         (*sp->lzw_hordiff)((char *)op0, occ0, sp->lzw_stride);
  788.         return (1);
  789.     } else
  790.         return (0);
  791. }
  792.  
  793. /*
  794.  * Decode a tile/strip and apply the predictor routine.
  795.  * Note that horizontal differencing must be done on a
  796.  * row-by-row basis.  The width of a "row" has already
  797.  * been calculated at pre-decode time according to the
  798.  * strip/tile dimensions.
  799.  */
  800. static
  801. DECLARE4(LZWDecodePredTile, TIFF*, tif, u_char*, op0, u_long, occ0, u_int, s)
  802. {
  803.     LZWDecodeState *sp = (LZWDecodeState *)tif->tif_data;
  804.     u_long rowsize;
  805.  
  806.     assert(sp != NULL);
  807.     assert(sp->dec_decode != NULL);
  808.     if (!(*sp->dec_decode)(tif, op0, occ0, s))
  809.         return (0);
  810.     rowsize = sp->lzw_rowsize;
  811.     assert(rowsize > 0);
  812.     while ((long)occ0 > 0) {
  813.         (*sp->lzw_hordiff)((char*)op0, rowsize, sp->lzw_stride);
  814.         occ0 -= rowsize;
  815.         op0 += rowsize;
  816.     }
  817.     return (1);
  818. }
  819.  
  820. /*
  821.  * LZW Encoding.
  822.  */
  823.  
  824. static void
  825. DECLARE3(horizontalDifference8,
  826.     register char*, cp,
  827.     register u_long, cc,
  828.     register int, stride
  829. )
  830. {
  831.     if (cc > stride) {
  832.         cc -= stride;
  833.         /*
  834.          * Pipeline the most common cases.
  835.          */
  836.         if (stride == 3) {
  837.             int r1, g1, b1;
  838.             int r2 = cp[0];
  839.             int g2 = cp[1];
  840.             int b2 = cp[2];
  841.             do {
  842.                 r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
  843.                 g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
  844.                 b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
  845.                 cp += 3;
  846.             } while ((long)(cc -= 3) > 0);
  847.         } else if (stride == 4) {
  848.             int r1, g1, b1, a1;
  849.             int r2 = cp[0];
  850.             int g2 = cp[1];
  851.             int b2 = cp[2];
  852.             int a2 = cp[3];
  853.             do {
  854.                 r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
  855.                 g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
  856.                 b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
  857.                 a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
  858.                 cp += 4;
  859.             } while ((long)(cc -= 4) > 0);
  860.         } else {
  861.             cp += cc - 1;
  862.             do {
  863.                 REPEAT4(stride, cp[stride] -= cp[0]; cp--)
  864.             } while ((long)(cc -= stride) > 0);
  865.         }
  866.     }
  867. }
  868.  
  869. static void
  870. DECLARE3(horizontalDifference16,
  871.     char*, cp,
  872.     u_long, cc,
  873.     register int, stride
  874. )
  875. {
  876.     register short *wp = (short *)cp;
  877.     register u_long wc = cc/2;
  878.  
  879.     if (wc > stride) {
  880.         wc -= stride;
  881.         wp += wc - 1;
  882.         do {
  883.             REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  884.             wc -= stride;
  885.         } while (wc > 0);
  886.     }
  887. }
  888.  
  889. /*
  890.  * Reset encoding state at the start of a strip.
  891.  */
  892. static
  893. DECLARE1(LZWPreEncode, TIFF*, tif)
  894. {
  895.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  896.  
  897.     if (sp == NULL) {
  898.         tif->tif_data = _TIFFmalloc(sizeof (LZWEncodeState));
  899.         if (tif->tif_data == NULL) {
  900.             TIFFError("LZWPreEncode",
  901.                 "No space for LZW state block");
  902.             return (0);
  903.         }
  904.         sp = (LZWEncodeState *)tif->tif_data;
  905.         if (!LZWCheckPredictor(tif, &sp->base,
  906.             horizontalDifference8, horizontalDifference16))
  907.             return (0);
  908.         if (sp->lzw_hordiff != NULL) {
  909.             tif->tif_encoderow = LZWEncodePredRow;
  910.             tif->tif_encodestrip = LZWEncodePredTile;
  911.             tif->tif_encodetile = LZWEncodePredTile;
  912.         }
  913.     }
  914.     sp->lzw_nbits = BITS_MIN;
  915.     sp->lzw_maxcode = MAXCODE(BITS_MIN);
  916.     sp->lzw_free_ent = CODE_FIRST;
  917.     sp->lzw_nextbits = 0;
  918.     sp->lzw_nextdata = 0;
  919.     sp->enc_checkpoint = CHECK_GAP;
  920.     sp->enc_ratio = 0;
  921.     sp->enc_incount = 0;
  922.     sp->enc_outcount = 0;
  923.     /*
  924.      * The 4 here insures there is space for 2 max-sized
  925.      * codes in LZWEncode and LZWPostDecode.
  926.      */
  927.     sp->enc_rawlimit = tif->tif_rawdata + tif->tif_rawdatasize-1 - 4;
  928.     cl_hash(sp);        /* clear hash table */
  929.     sp->enc_oldcode = -1;    /* generates CODE_CLEAR in LZWEncode */
  930.     return (1);
  931. }
  932.  
  933. #define    CALCRATIO(sp, rat) {                    \
  934.     if (incount > 0x007fffff) { /* NB: shift will overflow */\
  935.         rat = outcount >> 8;                \
  936.         rat = (rat == 0 ? 0x7fffffff : incount/rat);    \
  937.     } else                            \
  938.         rat = (incount<<8) / outcount;            \
  939. }
  940. #define    PutNextCode(op, c) {                    \
  941.     nextdata = (nextdata << nbits) | c;            \
  942.     nextbits += nbits;                    \
  943.     *op++ = nextdata >> (nextbits-8);            \
  944.     nextbits -= 8;                        \
  945.     if (nextbits >= 8) {                    \
  946.         *op++ = nextdata >> (nextbits-8);        \
  947.         nextbits -= 8;                    \
  948.     }                            \
  949.     outcount += nbits;                    \
  950. }
  951.  
  952. /*
  953.  * Encode a chunk of pixels.
  954.  *
  955.  * Uses an open addressing double hashing (no chaining) on the 
  956.  * prefix code/next character combination.  We do a variant of
  957.  * Knuth's algorithm D (vol. 3, sec. 6.4) along with G. Knott's
  958.  * relatively-prime secondary probe.  Here, the modular division
  959.  * first probe is gives way to a faster exclusive-or manipulation. 
  960.  * Also do block compression with an adaptive reset, whereby the
  961.  * code table is cleared when the compression ratio decreases,
  962.  * but after the table fills.  The variable-length output codes
  963.  * are re-sized at this point, and a CODE_CLEAR is generated
  964.  * for the decoder. 
  965.  */
  966. static
  967. DECLARE4(LZWEncode, TIFF*, tif, u_char*, bp, u_long, cc, u_int, s)
  968. {
  969.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  970.     register long fcode;
  971.     register hash_t *hp;
  972.     register int h, c, ent, disp;
  973.     long incount, outcount, checkpoint;
  974.     long nextdata, nextbits;
  975.     int free_ent, maxcode, nbits;
  976.     char *op, *limit;
  977.  
  978.     if (sp == NULL)
  979.         return (0);
  980.     /*
  981.      * Load local state.
  982.      */
  983.     incount = sp->enc_incount;
  984.     outcount = sp->enc_outcount;
  985.     checkpoint = sp->enc_checkpoint;
  986.     nextdata = sp->lzw_nextdata;
  987.     nextbits = sp->lzw_nextbits;
  988.     free_ent = sp->lzw_free_ent;
  989.     maxcode = sp->lzw_maxcode;
  990.     nbits = sp->lzw_nbits;
  991.     op = tif->tif_rawcp;
  992.     limit = sp->enc_rawlimit;
  993.     ent = sp->enc_oldcode;
  994.  
  995.     if (ent == -1 && cc > 0) {
  996.         /*
  997.          * NB: This is safe because it can only happen
  998.          *     at the start of a strip where we know there
  999.          *     is space in the data buffer.
  1000.          */
  1001.         PutNextCode(op, CODE_CLEAR);
  1002.         ent = *bp++; cc--; incount++;
  1003.     }
  1004.     while (cc > 0) {
  1005.         c = *bp++; cc--; incount++;
  1006.         fcode = ((long)c << BITS_MAX) + ent;
  1007.         h = (c << HSHIFT) ^ ent;    /* xor hashing */
  1008.         hp = &sp->enc_hashtab[h];
  1009.         if (hp->hash == fcode) {
  1010.             ent = hp->code;
  1011.             continue;
  1012.         }
  1013.         if (hp->hash >= 0) {
  1014.             /*
  1015.              * Primary hash failed, check secondary hash.
  1016.              */
  1017.             disp = HSIZE - h;
  1018.             if (h == 0)
  1019.                 disp = 1;
  1020.             do {
  1021.                 if ((hp -= disp) < sp->enc_hashtab)
  1022.                     hp += HSIZE;
  1023.                 if (hp->hash == fcode) {
  1024.                     ent = hp->code;
  1025.                     goto hit;
  1026.                 }
  1027.             } while (hp->hash >= 0);
  1028.         }
  1029.         /*
  1030.          * New entry, emit code and add to table.
  1031.          */
  1032.         /*
  1033.          * Verify there is space in the buffer for the code
  1034.          * and any potential Clear code that might be emitted
  1035.          * below.  The value of limit is setup so that there
  1036.          * are at least 4 bytes free--room for 2 codes.
  1037.          */
  1038.         if (op > limit) {
  1039.             tif->tif_rawcc = op - tif->tif_rawdata;
  1040.             TIFFFlushData1(tif);
  1041.             op = tif->tif_rawdata;
  1042.         }
  1043.         PutNextCode(op, ent);
  1044.         ent = c;
  1045.         hp->code = free_ent++;
  1046.         hp->hash = fcode;
  1047.         if (free_ent == CODE_MAX-1) {
  1048.             /* table is full, emit clear code and reset */
  1049.             cl_hash(sp);
  1050.             sp->enc_ratio = 0;
  1051.             incount = 0;
  1052.             outcount = 0;
  1053.             free_ent = CODE_FIRST;
  1054.             PutNextCode(op, CODE_CLEAR);
  1055.             nbits = BITS_MIN;
  1056.             maxcode = MAXCODE(BITS_MIN);
  1057.         } else {
  1058.             /*
  1059.              * If the next entry is going to be too big for
  1060.              * the code size, then increase it, if possible.
  1061.              */
  1062.             if (free_ent > maxcode) {
  1063.                 nbits++;
  1064.                 assert(nbits <= BITS_MAX);
  1065.                 maxcode = MAXCODE(nbits);
  1066.             } else if (incount >= checkpoint) {
  1067.                 long rat;
  1068.                 /*
  1069.                  * Check compression ratio and, if things seem
  1070.                  * to be slipping, clear the hash table and
  1071.                  * reset state.  The compression ratio is a
  1072.                  * 24+8-bit fractional number.
  1073.                  */
  1074.                 checkpoint = incount+CHECK_GAP;
  1075.                 CALCRATIO(sp, rat);
  1076.                 if (rat <= sp->enc_ratio) {
  1077.                     cl_hash(sp);
  1078.                     sp->enc_ratio = 0;
  1079.                     incount = 0;
  1080.                     outcount = 0;
  1081.                     free_ent = CODE_FIRST;
  1082.                     PutNextCode(op, CODE_CLEAR);
  1083.                     nbits = BITS_MIN;
  1084.                     maxcode = MAXCODE(BITS_MIN);
  1085.                 } else
  1086.                     sp->enc_ratio = rat;
  1087.             }
  1088.         }
  1089.     hit:
  1090.         ;
  1091.     }
  1092.  
  1093.     /*
  1094.      * Restore global state.
  1095.      */
  1096.     sp->enc_incount = incount;
  1097.     sp->enc_outcount = outcount;
  1098.     sp->enc_checkpoint = checkpoint;
  1099.     sp->enc_oldcode = ent;
  1100.     sp->lzw_nextdata = nextdata;
  1101.     sp->lzw_nextbits = nextbits;
  1102.     sp->lzw_free_ent = free_ent;
  1103.     sp->lzw_maxcode = maxcode;
  1104.     sp->lzw_nbits = nbits;
  1105.     tif->tif_rawcp = op;
  1106.     return (1);
  1107. }
  1108.  
  1109. static
  1110. DECLARE4(LZWEncodePredRow, TIFF*, tif, u_char*, bp, u_long, cc, u_int, s)
  1111. {
  1112.     LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1113.  
  1114.     assert(sp != NULL);
  1115.     assert(sp->lzw_hordiff != NULL);
  1116. /* XXX horizontal differencing alters user's data XXX */
  1117.     (*sp->lzw_hordiff)((char *)bp, cc, sp->lzw_stride);
  1118.     return (LZWEncode(tif, bp, cc, s));
  1119. }
  1120.  
  1121. static
  1122. DECLARE4(LZWEncodePredTile, TIFF*, tif, u_char*, bp0, u_long, cc0, u_int, s)
  1123. {
  1124.     LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1125.     u_long cc = cc0, rowsize;
  1126.     u_char *bp = bp0;
  1127.  
  1128.     assert(sp != NULL);
  1129.     assert(sp->lzw_hordiff != NULL);
  1130.     rowsize = sp->lzw_rowsize;
  1131.     assert(rowsize > 0);
  1132.     while ((long)cc > 0) {
  1133.         (*sp->lzw_hordiff)((char *)bp, rowsize, sp->lzw_stride);
  1134.         cc -= rowsize;
  1135.         bp += rowsize;
  1136.     }
  1137.     return (LZWEncode(tif, bp0, cc0, s));
  1138. }
  1139.  
  1140. /*
  1141.  * Finish off an encoded strip by flushing the last
  1142.  * string and tacking on an End Of Information code.
  1143.  */
  1144. static
  1145. DECLARE1(LZWPostEncode, TIFF*, tif)
  1146. {
  1147.     register LZWEncodeState *sp = (LZWEncodeState *)tif->tif_data;
  1148.     char *op = tif->tif_rawcp;
  1149.     long nextbits = sp->lzw_nextbits;
  1150.     long nextdata = sp->lzw_nextdata;
  1151.     long outcount = sp->enc_outcount;
  1152.     int nbits = sp->lzw_nbits;
  1153.  
  1154.     if (op > sp->enc_rawlimit) {
  1155.         tif->tif_rawcc = op - tif->tif_rawdata;
  1156.         TIFFFlushData1(tif);
  1157.         op = tif->tif_rawdata;
  1158.     }
  1159.     if (sp->enc_oldcode != -1) {
  1160.         PutNextCode(op, sp->enc_oldcode);
  1161.         sp->enc_oldcode = -1;
  1162.     }
  1163.     PutNextCode(op, CODE_EOI);
  1164.     if (nextbits > 0) 
  1165.         *op++ = nextdata << (8-nextbits);
  1166.     tif->tif_rawcc = op - tif->tif_rawdata;
  1167.     return (1);
  1168. }
  1169.  
  1170. /*
  1171.  * Reset encoding hash table.
  1172.  */
  1173. static void
  1174. DECLARE1(cl_hash, LZWEncodeState*, sp)
  1175. {
  1176.     register hash_t *hp = &sp->enc_hashtab[HSIZE-1];
  1177.     register long i = HSIZE-8;
  1178.  
  1179.      do {
  1180.         i -= 8;
  1181.         hp[-7].hash = -1;
  1182.         hp[-6].hash = -1;
  1183.         hp[-5].hash = -1;
  1184.         hp[-4].hash = -1;
  1185.         hp[-3].hash = -1;
  1186.         hp[-2].hash = -1;
  1187.         hp[-1].hash = -1;
  1188.         hp[ 0].hash = -1;
  1189.         hp -= 8;
  1190.     } while (i >= 0);
  1191.         for (i += 8; i > 0; i--, hp--)
  1192.         hp->hash = -1;
  1193. }
  1194.  
  1195. static
  1196. DECLARE1(LZWCleanup, TIFF*, tif)
  1197. {
  1198.     if (tif->tif_data) {
  1199.         _TIFFfree(tif->tif_data);
  1200.         tif->tif_data = NULL;
  1201.     }
  1202. }
  1203.  
  1204. int
  1205. DECLARE1(TIFFInitLZW, TIFF*, tif)
  1206. {
  1207.     tif->tif_predecode = LZWPreDecode;
  1208.     tif->tif_decoderow = LZWDecode;
  1209.     tif->tif_decodestrip = LZWDecode;
  1210.     tif->tif_decodetile = LZWDecode;
  1211.     tif->tif_preencode = LZWPreEncode;
  1212.     tif->tif_postencode = LZWPostEncode;
  1213.     tif->tif_encoderow = LZWEncode;
  1214.     tif->tif_encodestrip = LZWEncode;
  1215.     tif->tif_encodetile = LZWEncode;
  1216.     tif->tif_cleanup = LZWCleanup;
  1217.     return (1);
  1218. }
  1219.  
  1220. /*
  1221.  * Copyright (c) 1985, 1986 The Regents of the University of California.
  1222.  * All rights reserved.
  1223.  *
  1224.  * This code is derived from software contributed to Berkeley by
  1225.  * James A. Woods, derived from original work by Spencer Thomas
  1226.  * and Joseph Orost.
  1227.  *
  1228.  * Redistribution and use in source and binary forms are permitted
  1229.  * provided that the above copyright notice and this paragraph are
  1230.  * duplicated in all such forms and that any documentation,
  1231.  * advertising materials, and other materials related to such
  1232.  * distribution and use acknowledge that the software was developed
  1233.  * by the University of California, Berkeley.  The name of the
  1234.  * University may not be used to endorse or promote products derived
  1235.  * from this software without specific prior written permission.
  1236.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  1237.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  1238.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  1239.  */
  1240.